package org.blaznyoght.subtitles.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JSpinner.DateEditor;
import javax.swing.SpinnerDateModel;
import javax.swing.border.EmptyBorder;

import org.blaznyoght.subtitles.model.Time;

public class SynchronizeDialog extends JDialog {

	private static final String FORMAT = "hh:mm:ss,SSS";
	/**
	 * 
	 */
	private static final long serialVersionUID = -6749739993878715639L;

	private final JPanel contentPanel = new JPanel();

	private SwingApplication application;

	private final SpinnerDateModel startModel = new SpinnerDateModel();
	private final JSpinner startSpinner = new JSpinner(startModel);
	private final DateEditor startEditor = new DateEditor(startSpinner, FORMAT);
	private final SpinnerDateModel endModel = new SpinnerDateModel();
	private final JSpinner endSpinner = new JSpinner(endModel);
	private final DateEditor endEditor = new DateEditor(endSpinner, FORMAT);
	private JLabel lblStartTime;
	
	private Integer startRank;
	private Integer endRank;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			SynchronizeDialog dialog = new SynchronizeDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public SynchronizeDialog() {
		setResizable(false);
		init();
	}

	/**
	 * Create the dialog.
	 */
	public SynchronizeDialog(SwingApplication application) {
		super(application.getFrame());
		setApplication(application);
		init();
	}

	private void init() {
		setBounds(100, 100, 450, 200);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		GridBagLayout gbl_contentPanel = new GridBagLayout();
		gbl_contentPanel.columnWidths = new int[] { 133, 300, 0 };
		gbl_contentPanel.rowHeights = new int[] { 28, 0, 0 };
		gbl_contentPanel.columnWeights = new double[] { 0.0, 1.0,
				Double.MIN_VALUE };
		gbl_contentPanel.rowWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
		contentPanel.setLayout(gbl_contentPanel);
		{
			lblStartTime = new JLabel("Start time");
			GridBagConstraints gbc_lblStartTime = new GridBagConstraints();
			gbc_lblStartTime.anchor = GridBagConstraints.WEST;
			gbc_lblStartTime.insets = new Insets(0, 0, 5, 5);
			gbc_lblStartTime.gridx = 0;
			gbc_lblStartTime.gridy = 0;
			contentPanel.add(lblStartTime, gbc_lblStartTime);
		}
		startSpinner.setEditor(startEditor);
		GridBagConstraints gbc_startSpinner = new GridBagConstraints();
		gbc_startSpinner.fill = GridBagConstraints.HORIZONTAL;
		gbc_startSpinner.insets = new Insets(0, 0, 5, 0);
		gbc_startSpinner.anchor = GridBagConstraints.BASELINE;
		gbc_startSpinner.gridx = 1;
		gbc_startSpinner.gridy = 0;
		contentPanel.add(startSpinner, gbc_startSpinner);
		{
			JLabel label = new JLabel("End time");
			GridBagConstraints gbc_label = new GridBagConstraints();
			gbc_label.insets = new Insets(0, 0, 0, 5);
			gbc_label.anchor = GridBagConstraints.WEST;
			gbc_label.gridx = 0;
			gbc_label.gridy = 1;
			contentPanel.add(label, gbc_label);
		}
		endSpinner.setEditor(endEditor);
		GridBagConstraints gbc_endSpinner = new GridBagConstraints();
		gbc_endSpinner.fill = GridBagConstraints.HORIZONTAL;
		gbc_endSpinner.anchor = GridBagConstraints.NORTH;
		gbc_endSpinner.gridx = 1;
		gbc_endSpinner.gridy = 1;
		contentPanel.add(endSpinner, gbc_endSpinner);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				okButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						application.synchronize(
								getStartRank(), 
								new Time((Date) startSpinner.getValue()), 
								getEndRank(), 
								new Time((Date) endSpinner.getValue()));
						SynchronizeDialog.this.dispose();
					}
				});
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						SynchronizeDialog.this.dispose();
					}
				});
				buttonPane.add(cancelButton);
			}
		}
	}

	public JSpinner getStartSpinner() {
		return this.startSpinner;
	}

	public JSpinner getEndSpinner() {
		return this.endSpinner;
	}

	/**
	 * @return the application
	 */
	public SwingApplication getApplication() {
		return application;
	}

	/**
	 * @param application
	 *            the application to set
	 */
	public void setApplication(SwingApplication application) {
		this.application = application;
	}

	/**
	 * @return the startRank
	 */
	public Integer getStartRank() {
		return startRank;
	}

	/**
	 * @return the endRank
	 */
	public Integer getEndRank() {
		return endRank;
	}

	public void setStartRank(Integer long1) {
		this.startRank = long1;
	}

	public void setEndRank(Integer long1) {
		this.endRank = long1;
	}

}
